using ASP;
using IPMS.BLL;
using System;
using System.Configuration;
using System.Globalization;
using System.IO;
using System.Threading;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;


public partial class ProjectSearchPrint : Page, IRequiresSessionState
{
    protected void CreateFolder(string path)
    {
        try
        {
            try
            {
                if (!Directory.Exists(path))
                {
                    DirectoryInfo di = Directory.CreateDirectory(path);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("The process failed: {0}", e.ToString());
            }
        }
        finally
        {
        }
    }
    protected void btnExport_Click1(object sender, EventArgs e)
    {
        this.CreateFolder(base.Server.MapPath("~/Excel/"));
        this.ExportToExcel();
        FileInfo info = new FileInfo(base.Server.MapPath("~/Excel/ExcelFilename.xls"));
        base.Response.Clear();
        base.Response.AddHeader("Content-Disposition", "attachment; filename=ExcelFilename.xls");
        base.Response.AddHeader("Content-Length", info.Length.ToString());
        base.Response.ContentType = "application/vnd.xls";
        base.Response.WriteFile(info.FullName);
        base.Response.End();
    }

    private void ExportToExcel()
    {
        try
        {
            string strFilePath = string.Empty;
            strFilePath = base.Server.MapPath(@"Excel\") + "ExcelFileName.xls";
            if (File.Exists(strFilePath))
            {
                File.Delete(strFilePath);
            }
            StringWriter oStringWriter = new StringWriter();
            HtmlTextWriter oHtmlTextWriter = new HtmlTextWriter(oStringWriter);
            StreamWriter objStreamWriter = File.AppendText(strFilePath);
            this.gvResults.RenderControl(oHtmlTextWriter);
            objStreamWriter.WriteLine(oStringWriter.ToString());
            objStreamWriter.Close();
        }
        catch (Exception)
        {
        }
    }

    protected void gvResults_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[11].Text = IPDataFormat.FormatDateTime(e.Row.Cells[11].Text);
        }
    }

    protected override void InitializeCulture()
    {
        string sCulture = ConfigurationManager.AppSettings["DefaultCulture"].ToString();
        try
        {
            sCulture = this.Session["Culture"].ToString();
        }
        catch (NullReferenceException)
        {
        }
        Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(sCulture);
        Thread.CurrentThread.CurrentUICulture = new CultureInfo(sCulture);
        base.InitializeCulture();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        this.txtStartDate.Text = IPDataFormat.ConvertDateFormat(base.Request.QueryString["StartingNextDueDate"].ToString(), 1);
        this.txtEndDate.Text = IPDataFormat.ConvertDateFormat(base.Request.QueryString["EndingNextDueDate"].ToString(), 1);
        this.txtUserName.Text = this.Page.User.Identity.Name.ToString();
    }

    public override void VerifyRenderingInServerForm(Control control)
    {
    }
}
